﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using System.Xml;

namespace GPBenhLanh
{
    public partial class frmChiDinhHM : DevExpress.XtraEditors.XtraForm
    {
        public frmChiDinhHM()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btngo_Click(object sender, EventArgs e)
        {
            string SoTieuBan = txtSoTieuBan.Text;
            if (SoTieuBan.Equals(""))
                return;
            LoadGrid();
            DataTable dt = GetThongTinSearchBUS.SearchDaXacNhanBySoTB(SoTieuBan);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    btnNhapChiDinh.Enabled = true;
                    txtHoTen.Text = dt.Rows[0]["HoTen"].ToString();
                    txtTuoi.Text = dt.Rows[0]["namsinh"].ToString()
                                 + "(" + dt.Rows[0]["tuoi"].ToString() + " Tuổi)";
                    txtGioi.Text = dt.Rows[0]["gioi"].ToString();
                    txtdiachi.Text = dt.Rows[0]["DiaChi"].ToString();                    
                    txtsohs.Text = dt.Rows[0]["SoHS"].ToString();
                    txtsobn.Text = dt.Rows[0]["SoBN"].ToString();
                    txtSoNVien.Text = dt.Rows[0]["sonhapvien"].ToString();
                    txtChuanDoan.Text = dt.Rows[0]["chandoan"].ToString();
                    comptv.EditValue = int.Parse(dt.Rows[0]["ptvid"].ToString());
                    //da co chi dinh
                    DataTable dtCDThem = GetThongTinSearchBUS.GetChiDinhThem(txtSoTieuBan.Text);
                    DataTable dtNhuomDB = (DataTable)gridMain.DataSource;
                    DataTable dtHMMD = (DataTable)grdHMMD.DataSource;
                    if (dtCDThem.Rows.Count > 0)
                    {
                        btnNhapChiDinh.Enabled = false;
                        btnBoXacNhan.Enabled = true;
                        
                        for (int i = 0; i < dtCDThem.Rows.Count; i++)
                        {
                            for (int j = 0; j < dtHMMD.Rows.Count; j++)
                            {
                                if (dtCDThem.Rows[i]["IDHM"].ToString() == dtHMMD.Rows[j]["IDNhom"].ToString())
                                {
                                    dtHMMD.Rows[j]["Checked"] = 1;
                                }                                
                            }

                            for (int j = 0; j < dtNhuomDB.Rows.Count; j++)
                            {
                                if (dtCDThem.Rows[i]["IDHM"].ToString() == dtNhuomDB.Rows[j]["IDNhuomDB"].ToString())
                                {
                                    dtNhuomDB.Rows[j]["Checked"] = 1;
                                }                                
                            }
                            txtMauBenhPham.Text = dtCDThem.Rows[i]["MauBenhPham"].ToString();
                            txtChanDoanGPB.Text = dtCDThem.Rows[i]["ChanDoanGPB"].ToString();
                        }                         
                    }
                    else
                    {
                        txtMauBenhPham.Text = "";
                        txtChanDoanGPB.Text = "";
                    }
                }
                else
                {
                    txtHoTen.Text = "";
                    txtTuoi.Text = "";
                    txtGioi.Text = "";
                    txtdiachi.Text = "";                    
                    txtsohs.Text = "";
                    txtsobn.Text = "";
                    txtMauBenhPham.Text ="";
                    txtChanDoanGPB.Text = "";

                }
            }
        }

        private void initControl(Control ctrl)
        {

            // Bỏ qua điều kiểm trừ khi nó là TextBox.
            if (ctrl.Tag == "text")
            {
                ctrl.Text = "";
            }

            // Xử lý các điều kiểm một cách đệ quy.
            // Điều này cần thiết khi có một điều kiểm chứa nhiều 
            // điều kiểm khác (ví dụ, khi bạn sử dụng Panel, 
            // GroupBox, hoặc điều kiểm container nào khác).
            foreach (Control ctrlChild in ctrl.Controls)
            {
                initControl(ctrlChild);
            }
        }

        private void LoadGrid()
        {
            DataTable dtNhuomDB = GetThongTinSearchBUS.GetNhuomDB();
            gridMain.DataSource = dtNhuomDB;
            //dsXacNhan ds = new dsXacNhan();

            DataTable dtHMMD = GetThongTinSearchBUS.GetHMMD();
            grdHMMD.DataSource = dtHMMD;

            DataTable dtBSChiDinh = GetThongTinSearchBUS.SearchDSBacSi();
            comBacSi.Properties.DataSource = dtBSChiDinh;
        }
        private void frmChiDinhHM_Load(object sender, EventArgs e)
        {
            initControl(this);
            btnNhapChiDinh.Enabled = false;
            btnBoXacNhan.Enabled = false;

            LoadGrid();
            comptv.Properties.DataSource = GetThongTinSearchBUS.SearchDSBacSiCD();
        }

        private void btnNhapChiDinh_Click(object sender, EventArgs e)
        {
            //lay danh sach nhuom dac biet
            DataTable DataSource = (DataTable)gridMain.DataSource;
            DataTable dtChanger = DataSource.GetChanges(DataRowState.Modified);
            string IDHM = "";
            int loai = 0;
            string UserName = "";
            string Host = Uit.it_Computer.ComputerName().ToString();
            int idBS = int.Parse(comBacSi.EditValue.ToString());
            string TenBS = comBacSi.Text;
            if (dtChanger != null)
            {                
                for (int i = 0; i < dtChanger.Rows.Count; i++)
                {
                    if (dtChanger.Rows[i]["Checked"].ToString() == "1")
                    {
                        loai = 1;
                        IDHM = dtChanger.Rows[i]["IDNhuomDB"].ToString();
                        KQBUS.InsertCDThem(txtSoTieuBan.Text, IDHM, loai, txtMauBenhPham.Text
                            , txtChanDoanGPB.Text, UserName, Host, idBS, TenBS);
                    }
                }
            }
            //lay danh sach nhuom HMMD
            DataTable DataSource2 = (DataTable)grdHMMD.DataSource;
            DataTable dtChanger2 = DataSource2.GetChanges(DataRowState.Modified);
            if (dtChanger2 != null)
            {
                for (int i = 0; i < dtChanger2.Rows.Count; i++)
                {
                    if (dtChanger2.Rows[i]["Checked"].ToString() == "1")
                    {
                        loai = 2;
                        IDHM = dtChanger2.Rows[i]["IDNhom"].ToString();
                        KQBUS.InsertCDThem(txtSoTieuBan.Text.ToUpper(), IDHM, loai
                            , txtMauBenhPham.Text, txtChanDoanGPB.Text
                           , UserName, Host, idBS, TenBS);
                    }
                }
            }
            btngo_Click(sender, e);
        }


        private void checkControl(rpfPhieuCDBoXung rp,string IDHM)
        {
            
            switch (IDHM)
            {
                case "TP29":
                    rp.TP29.Checked = true;
                break;

                case "TP30":
                    rp.TP30.Checked = true;
                break;

                case "TP31":
                    rp.TP31.Checked = true;
                break;

                case "TP32":
                    rp.TP32.Checked = true;
                break;

                case "PTHM101":
                    rp.PTHM101.Checked = true;
                break;

                case "PTHM102":
                    rp.PTHM102.Checked = true;
                break;

                case "PTHM103":
                    rp.PTHM103.Checked = true;
                break;

                case "PTHM104":
                    rp.PTHM104.Checked = true;
                break;

                case "PTHM105":
                    rp.PTHM105.Checked = true;
                break;

                case "PTHM106":
                    rp.PTHM106.Checked = true;
                break;

                case "PTHM107":
                    rp.PTHM107.Checked = true;
                break;

                case "PTHM108":
                rp.PTHM108.Checked = true;
                break;

                case "PTHM109":
                rp.PTHM109.Checked = true;
                break;
                
                case "PTHM110":
                rp.PTHM110.Checked = true;
                break;

                case "PTHM111":
                rp.PTHM111.Checked = true;
                break;
                
                case "PTHM112":
                rp.PTHM112.Checked = true;
                break;

                case "PTHM113":
                rp.PTHM113.Checked = true;
                break;

                case "PTHM114":
                rp.PTHM114.Checked = true;
                break;

                case "PTHM115":
                rp.PTHM115.Checked = true;
                break;

                case "PTHM116":
                rp.PTHM116.Checked = true;
                break;
                ///#########################
                case "PTHM201":
                rp.PTHM201.Checked = true;
                break;

                case "PTHM202":
                rp.PTHM202.Checked = true;
                break;

                case "PTHM203":
                rp.PTHM203.Checked = true;
                break;

                case "PTHM204":
                rp.PTHM204.Checked = true;
                break;

                case "PTHM205":
                rp.PTHM205.Checked = true;
                break;

                case "PTHM206":
                rp.PTHM206.Checked = true;
                break;

                case "PTHM207":
                rp.PTHM207.Checked = true;
                break;

                case "PTHM208":
                rp.PTHM208.Checked = true;
                break;

                case "PTHM209":
                rp.PTHM209.Checked = true;
                break;

                case "PTHM210":
                rp.PTHM210.Checked = true;
                break;

                case "PTHM211":
                rp.PTHM211.Checked = true;
                break;

                case "PTHM212":
                rp.PTHM212.Checked = true;
                break;

                case "PTHM213":
                rp.PTHM213.Checked = true;
                break;
                case "PTHM214":
                rp.PTHM214.Checked = true;
                break;

                case "PTHM215":
                rp.PTHM215.Checked = true;
                break;

                case "PTHM216":
                rp.PTHM216.Checked = true;
                break;
                //##############
                case "PTHM301":
                rp.PTHM301.Checked = true;
                break;

                case "PTHM302":
                rp.PTHM302.Checked = true;
                break;

                case "PTHM303":
                rp.PTHM303.Checked = true;
                break;

                case "PTHM304":
                rp.PTHM304.Checked = true;
                break;

                case "PTHM305":
                rp.PTHM305.Checked = true;
                break;

                case "PTHM306":
                rp.PTHM306.Checked = true;
                break;

                case "PTHM307":
                rp.PTHM307.Checked = true;
                break;

                case "PTHM308":
                rp.PTHM308.Checked = true;
                break;

                case "PTHM309":
                rp.PTHM309.Checked = true;
                break;

                case "PTHM310":
                rp.PTHM310.Checked = true;
                break;

                case "PTHM311":
                rp.PTHM311.Checked = true;
                break;

                case "PTHM312":
                rp.PTHM312.Checked = true;
                break;

                case "PTHM313":
                rp.PTHM313.Checked = true;
                break;

                case "PTHM314":
                rp.PTHM314.Checked = true;
                break;

                case "PTHM315":
                rp.PTHM315.Checked = true;
                break;

                case "PTHM316":
                rp.PTHM316.Checked = true;
                break;

                case "PTHM317":
                rp.PTHM317.Checked = true;
                break;

                case "PTHM318":
                rp.PTHM318.Checked = true;
                break;

                default:
                    break;
            }
            
        }

        private void btnInPhieu_Click(object sender, EventArgs e)
        {
            rpfPhieuCDBoXung rp = new rpfPhieuCDBoXung();
            DataTable dtCDThem = GetThongTinSearchBUS.GetChiDinhThem(txtSoTieuBan.Text);
            string IDHM = "";
            int Tien = 0;
            int TienDB = 0;
            int TienHM = 0;
            int DemDB = 0;
            int DemHM = 0;
            string bsCD ="";
            DateTime CDate = new DateTime ();
            if (dtCDThem != null)
            {
                for (int i = 0; i < dtCDThem.Rows.Count; i++)
                {
                    IDHM = dtCDThem.Rows[i]["IDHM"].ToString();
                    checkControl(rp, IDHM);
                    if(int.Parse(dtCDThem.Rows[i]["Nhom"].ToString())==0)
                    {
                        DemDB++;
                        TienDB += int.Parse(dtCDThem.Rows[i]["GiaTien"].ToString());
                    }
                    if (int.Parse(dtCDThem.Rows[i]["Nhom"].ToString()) != 0)
                    {
                        if (dtCDThem.Rows[i]["IDHM"].ToString() == "PTHM214")
                        {
                            DemHM += 2;
                        }
                        else if (dtCDThem.Rows[i]["IDHM"].ToString() == "PTHM215")
                        {
                            DemHM += 4;
                        }
                        else if (dtCDThem.Rows[i]["IDHM"].ToString() == "PTHM216")
                        {
                            DemHM += 8;
                        }
                        else
                        {
                            DemHM++;
                        }
                        TienHM += int.Parse(dtCDThem.Rows[i]["GiaTien"].ToString());
                    }
                    Tien    = TienDB + TienHM;
                    bsCD    = dtCDThem.Rows[i]["TenBSChiDinh"].ToString();
                    CDate   = DateTime.Parse(dtCDThem.Rows[i]["CDate"].ToString());
                }
                rp.txthoten.Text        = txtHoTen.Text;
                rp.txttuoi.Text         = txtTuoi.Text;
                rp.txtGioiTinh.Text     = txtGioi.Text;
                rp.txtDiaChi.Text       = txtdiachi.Text;
                rp.txtsonv.Text         = txtSoNVien.Text;
                rp.txtChanDoan.Text     = txtChuanDoan.Text;
                rp.txtptv.Text          = comptv.Text;
                rp.txtChanDoanGPB.Text  = txtChanDoanGPB.Text;
                rp.txtMauBP.Text        = txtMauBenhPham.Text;
                rp.txtSoTieuBan.Text    = txtSoTieuBan.Text.ToUpper();
                rp.barSoHS.Text         = txtsohs.Text;
                rp.txtSoBN.Text         = txtsobn.Text;

                rp.txtSoDB.Text         = DemDB.ToString("#0,#");
                rp.txtSoHoaMo.Text      = DemHM.ToString("#0,#");
                rp.txtTienDB.Text       = TienDB.ToString("#0,#");
                rp.txtTienHoaMo.Text    = TienHM.ToString("#0,#");
                rp.txtTongTien.Text     = Tien.ToString("#0,#");
                rp.txtBSCD.Text         = bsCD;
                rp.txtNgayCD.Text       = "Ngày " + CDate.Day.ToString()
                                        + " Tháng " + CDate.Month.ToString()
                                        + " Năm " + CDate.Year.ToString();
            }
            rp.ShowPreview();
        }

        private void btnBoXacNhan_Click(object sender, EventArgs e)
        {
           bool rs = KQBUS.DeleteGPB_CD_XNBOXUNG(txtSoTieuBan.Text);
           if (rs)
           {
               MessageBox.Show("Xóa thành công!", "Thông báo");
               btngo_Click(sender,e);
           }
        }

    }
}